// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database();
  const {
    count,
    goodId,
    mobile,
    good
  } = event;

 
  

  const data = await db.collection("wh04cart").where({
    mobile,
    goodId
  }).get();

  if(data.data.length>0){
     // 有  累计 加 count 
     const result = await db.collection("wh04cart").where({
      mobile,
      goodId
     })
     .update({
       data:{
         count: db.command.inc(count)
       }
     })
     return {
      ...result,
      event,
      msg:'累计增加数据成功'
    }
  }else{
    // 没有 就要新增  
    const result = await db.collection("wh04cart").add({
      data:{
        count,
        goodId,
        mobile,
        good,
        time:new Date()
      }
    })

    return {
      ...result,
      event,
      msg:'新增数据成功'
    }
  }


}